/**
 *  增加或减少(输入) 选择的商品
 */
define(function(require,exports,module) {
    var myPub = require('./public');

    SumPriceEvent();
    function  SumPriceEvent() {
        var oAmountBox = $('.js_amountBox');                    //数量框
        var oGoodsPrice = $('.js_goodsPrice');                  //产品价格
        var oSumNum = $('.js_sumNum');                          //总数
        var oSumPrice = $('.js_sumPrice');                      //总价格

        var oGoodsSizeList = $('.js_goodsSizeList');	        //产品规格列表
        var oGoodsSizeSide = $('.js_goodsSizeSide'); 		    // 全部弹层

        var minCount = $('.jsMinCount');                        // 起购份数
        var kucun = $(".kucun").attr("value");                  // 库存份数
        var oChooseNumArea = $('.js_chooseNumArea');            // 选择数量区域
        var oTaxDelArea = $('.js_taxDelArea');                  // 显示税费区域
        var oTaxSum = $('.js_taxSum');                          // 总的税费
        var oTaxAll = $('.js_taxAll');                          // 计算总的税费区域

        //最大限制和最小限制
        var maxNum = 99;
        var minNum = 1;
        var bodyScrollTime = null;

        //数量输入框
        oSumNum.focus(function() {
            // var _disHeight = oGoodsSizeList.outerHeight(true);
            // oGoodsSizeList.hide();
            // oGoodsSizeSide.css({'bottom': -_disHeight});
        });

       /* oSumNum.blur(function() {
            var psdScrollTap_timer = null;
            oGoodsSizeList.show();
            oGoodsSizeSide.css({'bottom': 0});
            clearTimeout(psdScrollTap_timer);
            psdScrollTap_timer = setTimeout(function () {
                window.scrollTo(0,document.documentElement.clientHeight);
            },100);
        });*/

       function toggleTax(){
           var ntaxPrice = oTaxSum.text()*1;
           if(ntaxPrice == 0){
               oTaxAll.hide()
           }else{
               oTaxAll.show()
           }
       }

        oAmountBox.each(function(i) {
            var oHandBtn = oAmountBox.eq(i).find('.js_handBtn');
            var oGoodsPriceTxt = oGoodsPrice.eq(i).text();
            var iNow = oSumNum.eq(i).val();
            var oGrayGoods = oAmountBox.eq(i).parents('.js_carListArea');         // 下架商品
            var oTaxDel = $('.js_taxDel');                  // 显示税费数字
            var taxRate = 0;                                // 税费单价
            var taxPriceNum = 0;                            // 税费金额
            if(oGrayGoods.find('.js_grayBox').length > 0){
                oAmountBox.eq(i).find('.js_grayGoods').show();
            }


            oHandBtn.hammer().off('tap');
            oHandBtn.hammer().on('tap',function(e) {
                var oshopCarTax = $(this).parents('.outProvince').siblings('.js_taxDelAreaCar').find('.js_taxDelCar'); // 购物车页面
                var taxRateCar = 0;
                if(oshopCarTax && oshopCarTax.length>0){
                    taxRateCar = oshopCarTax.attr('taxrate')*1;
                }

                // 税费
                if(oTaxDelArea && oTaxDelArea.length > 0){
                    taxRate = $(this).parents('.js_chooseNumArea').siblings('.js_goodsSizeList').find('li.active').attr('taxfare');
                    if(taxRate && taxRate != undefined && taxRate!=''){
                        taxRate = taxRate*1;
                    }else{
                        taxRate = 0;
                    }
                }else{
                    taxRate = 0;
                }
                // 库存
                if(oChooseNumArea &&　oChooseNumArea.length > 0){
                    kucun = $(this).parents('.js_chooseNumArea').siblings('.js_goodsSmallImg').find('.kucun').text();
                }
                var nMaxNum = minCount.eq(i).attr('maxCount')*1;             // 限购最大数
                //oGoodsPrice.eq(i).html((oGoodsPriceTxt*iNow).toFixed(2));
                var minBuyCount = 1;
                if(minCount.eq(i).attr("minCount") != null && minCount.eq(i).attr("minCount") != undefined){
                    minBuyCount = parseInt(minCount.eq(i).attr("minCount"));
                }
                // 如果有限购的最大值存在且不为0 且小于等于99
                if(nMaxNum && nMaxNum !=0 && nMaxNum <= maxNum ){
                    maxNum = nMaxNum;
                }else{
                    maxNum = 99
                }
                // 单价
                var oneGoodsPrice = oGoodsPrice.eq(i).attr("pPrice");
                var oneJifen = oGoodsPrice.eq(i).attr("jifen");
                var index = oHandBtn.index(this);
                var _iNow = oSumNum.eq(i).val();
                oSumNum.blur();
                oHandBtn.removeClass('active');
                switch(index) {
                    //减少
                    case 0:
                        if(_iNow == minBuyCount*1+1 || _iNow == minBuyCount){
                            oHandBtn.eq(index).addClass('active')
                        }
                        if(_iNow <= minBuyCount) {
                            _iNow = minBuyCount;
                            myPub.TipLayer("不能再少了");
                        }
                        else {
                            _iNow--;
                        }
                        break;
                    //增加
                    case 1:
                        if(_iNow == maxNum || _iNow == maxNum*1-1 || _iNow == kucun*1-1 || _iNow == kucun){
                            oHandBtn.eq(index).addClass('active')
                        }
                        if(_iNow > maxNum-1) {
                            _iNow = maxNum;
                            myPub.TipLayer("最多只能"+maxNum+"，不要太贪心哦");
                        }
                        else {
                            _iNow++;
                        }
                        break;
                }
                if(kucun && kucun.length>0){
                    if(_iNow > kucun*1){
                        _iNow = kucun;
                        myPub.TipLayer("库存不足哦");
                    }
                }

                //检测已经在购物车
                if($(this).hasClass('incart')){
                    var cartId = $(this).attr("cartItemId");
                    updateCart(cartId,_iNow,function(){
                        /*var ntax = 0;
                        var nsumPrice = oSumPrice.text()*1;*/
                        iNow = _iNow;

                        // 税费金额
                        taxPriceNum = (iNow*taxRate).toFixed(2)*1;
                        oTaxDel.text('￥'+taxPriceNum.toFixed(2));

                        oGoodsPrice.eq(i).html((oneGoodsPrice*iNow).toFixed(2));
                        oSumNum.eq(i).val(iNow);
                        oshopCarTax.text((taxRateCar*iNow).toFixed(2));

                        // 总的税费
                        myPub.ShopSumPrice($('.js_carList').find('.js_checkBox'),'.js_carListArea','.js_taxDelCar',oTaxSum);


                        myPub.ShopSumPrice($('.js_carList').find('.js_checkBox'),'.js_carListArea','.js_goodsPrice',oSumPrice);
                        // 税费是0就不显示税费
                        toggleTax();

                    },function(){
                        oSumNum.eq(i).val(iNow);
                    });
                }
                else{
                    iNow = _iNow;
                    //判断积分显示
                    var returnMoneyPercentage=$('.js_goodsPrice').attr("returnMoneyPercentage");
                    var oReturnMoney=$('#returnMoney');
                    var oCostMemberScore=$('#costMemberScore');
                    if(oneJifen != undefined){
                        if(oneGoodsPrice*iNow == 0){
                            oGoodsPrice.eq(i).html(oneJifen*iNow+'积分');
                        }else{
                            if(returnMoneyPercentage!=undefined && returnMoneyPercentage!=null && returnMoneyPercentage!=0  && oReturnMoney.length>0){
                                oGoodsPrice.eq(i).html((oneGoodsPrice*iNow).toFixed(2)+'+'+oneJifen*iNow+'积分');
                                oReturnMoney.html((returnMoneyPercentage*iNow*oneGoodsPrice/100).toFixed(2))
                            }else{
                                oGoodsPrice.eq(i).html((oneGoodsPrice*iNow).toFixed(2)+'+'+oneJifen*iNow+'积分');
                            }
                        }
                    }else{
                        // 税费金额
                        taxPriceNum = (iNow*taxRate).toFixed(2)*1;
                        oTaxDel.text('￥'+taxPriceNum.toFixed(2));

                        if(returnMoneyPercentage!=undefined && returnMoneyPercentage!=null && returnMoneyPercentage!=0  && oReturnMoney.length>0){
                            oGoodsPrice.eq(i).html((oneGoodsPrice*iNow).toFixed(2));
                            oReturnMoney.html((returnMoneyPercentage*iNow*oneGoodsPrice/100).toFixed(2))
                        }else{
                            oGoodsPrice.eq(i).html((oneGoodsPrice*iNow).toFixed(2));
                        }
                        if(oCostMemberScore.length>0){
                            var memberCostScore=oCostMemberScore.attr('costMemberScore');
                            oCostMemberScore.html(memberCostScore*iNow);
                        }

                    }

                    oSumNum.eq(i).val(iNow);

                }



            });

            oSumNum.eq(i).off('input propertychange');
            oSumNum.eq(i).on('input propertychange',function(e) {
                var oshopCarTax = $(this).parents('.outProvince').siblings('.js_taxDelAreaCar').find('.js_taxDelCar'); // 购物车页面
                var taxRateCar = 0;
                if(oshopCarTax && oshopCarTax.length>0){
                    taxRateCar = oshopCarTax.attr('taxrate')*1;
                }
                // 税费
                if(oTaxDelArea && oTaxDelArea.length > 0){
                    taxRate = $(this).parents('.js_chooseNumArea').siblings('.js_goodsSizeList').find('li.active').attr('taxfare');

                    if(taxRate && taxRate != undefined && taxRate!=''){
                        taxRate = taxRate*1;
                    }else{
                        taxRate = 0;
                    }
                }else{
                    taxRate = 0;
                }
                // 库存
                if(oChooseNumArea &&　oChooseNumArea.length > 0){
                    kucun = $(this).parents('.js_chooseNumArea').siblings('.js_goodsSmallImg').find('.kucun').text();
                }
                var sumThis = $(this);
                var reg = new RegExp("^[0-9]*$");               // 验证数字
                // 单价
                var oneGoodsPrice = oGoodsPrice.eq(i).attr("pPrice");
                var oneJifen = oGoodsPrice.eq(i).attr("jifen");

                oHandBtn.removeClass('active')

                var nMaxNum = minCount.eq(i).attr('maxCount')*1;             // 限购最大数
                // 如果有限购的最大值存在且不为0 切小于等于99
                if(nMaxNum && nMaxNum !=0 && nMaxNum <= maxNum ){
                    maxNum = nMaxNum;
                }else{
                    maxNum = 99
                }

                var minBuyCount = 1;
                if(minCount.eq(i).attr("minCount") != null && minCount.eq(i).attr("minCount") != undefined){
                    minBuyCount = parseInt(minCount.eq(i).attr("minCount"));
                }

                if(!reg.test(oSumNum.eq(i).val())){
                    myPub.TipLayer('输入正确数字');
                    oSumNum.eq(i).val(minBuyCount);

                    oHandBtn.eq(0).addClass('active');
                }else if(oSumNum.eq(i).val() <= 0){
                    oSumNum.eq(i).val(minBuyCount);
                }else{
                    var _iNow = oSumNum.eq(i).prop('value')*1;
                    // 设置最大值会导致底部上移
                    if(_iNow > maxNum-1) {
                        _iNow = maxNum;
                        oSumNum.eq(i).val(_iNow);
                        myPub.TipLayer("最多只能"+maxNum+"，不要太贪心哦");

                        oHandBtn.eq(1).addClass('active');
                    }
                    if(_iNow < minBuyCount) {
                        _iNow = minBuyCount;
                        oSumNum.eq(i).val(_iNow);
                        oHandBtn.eq(0).addClass('active');
                        myPub.TipLayer("不能再少了");
                    }
                    if(kucun && kucun.length>0){
                        if(_iNow > kucun){
                            _iNow = kucun;
                            oHandBtn.eq(1).addClass('active');
                            myPub.TipLayer("库存不足哦");
                        }
                    }
                    if(sumThis.hasClass('incart')){
                        var cartId = oSumNum.eq(i).attr("cartItemId");
                        updateCart(cartId,_iNow,function(){
                            iNow = _iNow;

                            // 税费金额
                            taxPriceNum = (iNow*taxRate).toFixed(2)*1;
                            oTaxDel.text('￥'+taxPriceNum.toFixed(2));

                            oGoodsPrice.eq(i).html((oneGoodsPrice*iNow).toFixed(2));

                            // 总的税费
                            myPub.ShopSumPrice($('.js_carList').find('.js_checkBox'),'.js_carListArea','.js_taxDelCar',oTaxSum);

                            myPub.ShopSumPrice($('.js_carList').find('.js_checkBox'),'.js_carListArea','.js_goodsPrice',oSumPrice);

                            // 税费是0就不显示税费
                            toggleTax();

                        },function(){
                            oSumNum.eq(i).val(iNow);
                        });
                    }else{

                        oSumNum.eq(i).val(_iNow);
                    }
                }
               /* myPub.ValiNum(oSumNum.eq(i),function() {
                    myPub.TipLayer('输入正确数字');
                    oSumNum.eq(i).val(1);

                    oHandBtn.eq(0).addClass('active');
                },function() {
                    var _iNow = oSumNum.eq(i).prop('value')*1;
                    // 设置最大值会导致底部上移
                    if(_iNow > maxNum-1) {
                        _iNow = maxNum;
                        oSumNum.eq(i).val(_iNow);
                        myPub.TipLayer("最多只能"+maxNum+"，不要太贪心哦");

                        oHandBtn.eq(1).addClass('active');
                    }
                    if(_iNow < minBuyCount) {
                        _iNow = minBuyCount;
                        oSumNum.eq(i).val(_iNow);
                        oHandBtn.eq(0).addClass('active');
                        myPub.TipLayer("不能再少了");
                    }
                    if(_iNow > kucun){
                        _iNow = kucun;
                        oHandBtn.eq(1).addClass('active');
                        myPub.TipLayer("库存不足哦");
                    }
                    if(sumThis.hasClass('incart')){
                        var cartId = oSumNum.eq(i).attr("cartItemId");
                        updateCart(cartId,_iNow,function(){
                            iNow = _iNow;
                            oGoodsPrice.eq(i).html((oneGoodsPrice*iNow).toFixed(2));

                            myPub.ShopSumPrice($('.js_carList').find('.js_checkBox'),'.js_carListArea','.js_goodsPrice',oSumPrice);
                        },function(){
                            oSumNum.eq(i).val(iNow);
                        });
                    }else{

                        oSumNum.eq(i).val(_iNow);
                    }
                });*/
                $('.layermmain').css({'top':25+'%'})

                var returnMoneyPercentage=$('.js_goodsPrice').attr("returnMoneyPercentage");
                var oReturnMoney=$('#returnMoney');
                var oCostMemberScore=$('#costMemberScore');
                //判断积分显示
                if(oneJifen != undefined){
                    if(oneGoodsPrice*oSumNum.eq(i).val() == 0){
                        oGoodsPrice.eq(i).html(oneJifen*(oSumNum.eq(i).val())+'积分');
                    }else{
                        if(returnMoneyPercentage!=undefined && returnMoneyPercentage!=null && returnMoneyPercentage!=0  && oReturnMoney.length>0){
                            oGoodsPrice.eq(i).html((oneGoodsPrice*oSumNum.eq(i).val()).toFixed(2)+'+'+oneJifen*(oSumNum.eq(i).val())+'积分');
                            oReturnMoney.html((oneGoodsPrice*returnMoneyPercentage*oSumNum.eq(i).val()/100).toFixed(2))
                        }else{
                            oGoodsPrice.eq(i).html((oneGoodsPrice*oSumNum.eq(i).val()).toFixed(2)+'+'+oneJifen*(oSumNum.eq(i).val())+'积分');
                        }
                    }
                }else{
                    // 税费金额
                    taxPriceNum = (oSumNum.eq(i).val()*taxRate).toFixed(2)*1;
                    oTaxDel.text('￥'+taxPriceNum.toFixed(2));

                    if( returnMoneyPercentage!=undefined && returnMoneyPercentage!=null  && returnMoneyPercentage!=0 && oReturnMoney.length>0){
                        oGoodsPrice.eq(i).html((oneGoodsPrice*oSumNum.eq(i).val()).toFixed(2));
                        oReturnMoney.html((oneGoodsPrice*returnMoneyPercentage*oSumNum.eq(i).val()/100).toFixed(2));
                    }else{
                        oGoodsPrice.eq(i).html((oneGoodsPrice*oSumNum.eq(i).val()).toFixed(2));
                        // 税费
                        oshopCarTax.html((taxRateCar*oSumNum.eq(i).val()).toFixed(2))


                        // 总的税费
                        myPub.ShopSumPrice($('.js_carList').find('.js_checkBox'),'.js_carListArea','.js_taxDelCar',oTaxSum);


                        myPub.ShopSumPrice($('.js_carList').find('.js_checkBox'),'.js_carListArea','.js_goodsPrice',oSumPrice);
                    }
                    if(oCostMemberScore.length>0){
                        var memberCostScore=oCostMemberScore.attr('costMemberScore');
                        oCostMemberScore.html(memberCostScore*oSumNum.eq(i).val());
                    }

                    /*oGoodsPrice.eq(i).html((oneGoodsPrice*oSumNum.eq(i).val()).toFixed(2));*/
                }
                // //详情价格动态改动
                // oGoodsPrice.eq(i).html((oneGoodsPrice * oSumNum.eq(i).val()).toFixed(2));
                e.preventDefault();
            });

        });



        /*
         oSumNum.on('focus',function() {
         $('.layermmain').css({'top':25+'%'})

         });
         oSumNum.on('blur',function() {
         $('.layermmain').css({'top':0})
         })
         */

        function updateCart(cartId,quantity,successfn,failfn){
            G_JSON_POST('/shop/cartItem/updateCart',{"cartId":cartId,"quantity":quantity},function(data){
             if(data.status == "success"){
             successfn();
             }else{
             failfn();
             myPub.TipLayer(data.message);
             }
             });



          /*  //静态测试   写入ajax时 删除下面
            var data = "success";
            if(data == "success"){
                successfn();
            }else{
                failfn();
                //myPub.TipLayer(data.message);
            }*/
        }

        function setFocusScrollTop () {

        }
    };

    exports.SumPriceEvent = SumPriceEvent;
});